package com.erp.warehouse.clouderp_warehouse.controller;

import com.erp.warehouse.clouderp_warehouse.pojo.inbound.InboundOrderCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.inbound.InboundOrderModify;
import com.erp.warehouse.clouderp_warehouse.pojo.serial.OrderSerial;
import com.erp.warehouse.clouderp_warehouse.service.inbound.InboundOrderService;
import com.erp.warehouse.clouderp_warehouse.util.UniqueOrderCode;
import org.comple.erp.pojo.ResultStatus;
import org.comple.erp.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 2025/10/30 15:18
 * author: 向远洋
 */
@RestController
@RequestMapping("/inbound")
public class InboundOrderController {
    @Autowired
    InboundOrderService service;
    @Autowired
    UniqueOrderCode uniqueOrderCode;
    @Autowired
    private InboundOrderService inboundOrderService;

    @Value("${auth.staffId}")
    String staffId;
    @Value("${auth.companyId}")
    String companyId;

    /**
     * 获取inbound列表分页数据，查询条件必须包含公司编码。并且只查询未删除的。
     * 可选的查询参数：一、时间范围beginTime-endTime；
     * 二、keyword：同时对供应商编码、入库单号、备注进行查询
     * 四、type： 业务类型：采购入库、销售退货入库、盘盈入库、调拨入库、其他入库
     *
     * @param params： 一个json字符串
     * @return AjaxResult：一个统一接口
     */
    @PostMapping("/page/{grade}")
    public AjaxResult getPageInfo(@RequestBody Map<String, Object> params, @PathVariable String grade) {
        //获取当期用户信息 -----------------------未完成-----------------------
        //审核人可以查看公司内所有已提交的入库单，其余工作人只能查看自己的所有
        if(grade.equals("staff")) {
            params.put("staffId", staffId);
        }else if(grade.equals("auditor")){ //"auditor"即为审核员视角
            params.put("status", 2); //可以查看当前公司中的所有已提交包括已审核的入库单
        }
        params.put("companyId", companyId);
        return AjaxResult.back(ResultStatus.SUCCESS, "获取入库单分页成功")
                .add("pageInfo", service.findInboundOrderList(params));
    }

    /**
     * 新增入库单，除了id、status、printNumber、isDel不需要传入参数。其他inboundOrder的属性比如传入非null值
     *
     * @param inboundOrder： 同样是一个json传，每个key都要和inboundOrder字段对应
     * @return
     */
//    @PostMapping("/add")
//    public AjaxResult addInbound(@RequestBody InboundOrder inboundOrder) {
//        //通过分布式锁获取到该公司下当前的订单号，保证公司下订单号唯一
//
//        if (service.addInboundOrder(inboundOrder)) return AjaxResult.back(ResultStatus.SUCCESS, "新增入库单成功");
//        else return AjaxResult.back(ResultStatus.ERROR, "新增入库单失败");
//    }

    /**
     * 逻辑删除入库单，需要传入id和staffNo，staffNo为创建该入库单的业务员（限制只能删除自己创建的入库单）
     *
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult deleteInbound(@RequestBody Map<String, Object> params) {
        params.put("staffId", staffId);
        params.put("companyId", companyId);
        service.deleteById(params);
        return AjaxResult.back(ResultStatus.SUCCESS, "删除入库单成功");
    }

    @PostMapping("/deleteList")
    public AjaxResult deleteInboundList(@RequestParam List<Long> ids) {
         //根据jwt获取当前用户id，并查询编号 ----------------------------未完成----------------------------
        service.deleteByIds(ids, staffId, companyId);
        return AjaxResult.back(ResultStatus.SUCCESS, "删除入库单列表成功");
    }




    /**
     * 将入库单新增和入库单明细新增统一
     * @param request
     * @return
     */
    @PostMapping("/create")
    public AjaxResult createInboundOrderWithItems(@RequestBody InboundOrderCreate request,
                                                  @RequestParam(value="orderSerialList", required = false) List<OrderSerial> orderSerialList) throws InterruptedException {
        //从jwt获取到用户，和相关公司code
        String companyCode = "CNY001";
        //获取到当前订单编号
        String code = "RK" + uniqueOrderCode.getCode(companyCode, "INBOUND");
        request.getInboundOrder().setId(UUID.randomUUID().toString());
        request.getInboundOrder().setCode(code);
        request.getInboundOrder().setStaffId(staffId);
        request.getInboundOrder().setCompanyId(companyId);
        service.createWithItems(request);
        return AjaxResult.back(ResultStatus.SUCCESS, "入库单创建成功").add("inboundOrderRequest", request);
    }

    @PostMapping("/get/{id}")
    public AjaxResult getInboundOrderWithItems(@PathVariable String id) {
        String companyCode = "CNY001";
        return AjaxResult.back(ResultStatus.SUCCESS, "获取入库单和入库明细成功！")
                .add("inboundOrderRequest", inboundOrderService.getInboundOrderRequest(id));
    }

    @PostMapping("/modify")
    public AjaxResult modifyInboundOrderWithItems(@RequestBody InboundOrderModify inboundOrderModify) {
        inboundOrderService.modifyWithItems(inboundOrderModify);
        return AjaxResult.back(ResultStatus.SUCCESS, "修改入库单成功！");
    }

    /**
     *入库单业务类型，如果是调拨，则不计算成本。
     */
    @PostMapping("/auditor")
    public AjaxResult auditorInboundOrderWithItems(@RequestBody InboundOrderCreate inboundOrderCreate) {
        inboundOrderService.auditorWithItems(inboundOrderCreate, companyId);
        return AjaxResult.back(ResultStatus.SUCCESS, "审核成功！");
    }


}